﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class RegionForm : Form
    {
        int uisID;
        TextBox tbNewRegion = new TextBox();
        tb_region region;
        bool updateRec = true;

        public RegionForm()
        {
            InitializeComponent();
        }

        private void RegionForm_Load(object sender, EventArgs e)
        {
            cbUIS.Items.AddRange(GetUISes());
        }

        private object[] GetUISes()
        {
            return DBClass.RESPContext.tb_UIs.Select(se => se.Name).Distinct().ToArray();
        }

        private void cbUIS_SelectedIndexChanged(object sender, EventArgs e)
        {
            uisID = GetUISID(cbUIS.Items[cbUIS.SelectedIndex].ToString());
            cbRegion.Items.Clear();
            cbRegion.Items.AddRange(UtilClass.FillCBRegions(uisID));
            tbPopulation.Text = string.Empty;
            tbAVPBefore.Text = string.Empty;
            cbRegion.SelectedIndex = -1;
        }

        private int GetUISID(string uisName)
        {
            return DBClass.RESPContext.tb_UIs.Where(wh => wh.Name == uisName).Select(se => se.ID).FirstOrDefault();
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            string rName = cbRegion.Items[cbRegion.SelectedIndex].ToString();
            region = DBClass.RESPContext.tb_regions.Where(wh => wh.Name == rName && wh.UISID == uisID).FirstOrDefault();
            if (region != null)
            {
                tbPopulation.Text = region.Population.Value.ToString();
                tbAVPBefore.Text = region.AVPQuantityBefore.Value.ToString();
            }
        }

        private void NewRegionButton_Click(object sender, EventArgs e)
        {
            tbNewRegion = new TextBox();
            tbNewRegion.Location = cbRegion.Location;
            tbNewRegion.Size = cbRegion.Size;
            cbRegion.Visible = false;
            tbPopulation.Text = string.Empty;
            tbAVPBefore.Text = string.Empty;
            this.Controls.Add(tbNewRegion);
            updateRec = false;
            region = null;
        }

        private void ButtonOK_Click(object sender, EventArgs e)
        {

            int population, avpBefore;
            Int32.TryParse(tbPopulation.Text, out population);
            Int32.TryParse(tbAVPBefore.Text, out avpBefore);

            if (updateRec)
            {
                region.AVPQuantityBefore = avpBefore;
                region.Population = population;
            }
            else
            {
                if (String.IsNullOrEmpty(tbNewRegion.Text) || DBClass.Regions.Where(wh => wh.Name == tbNewRegion.Text).Select(se=>se.Name).ToArray().Contains(tbNewRegion.Text))
                {
                    MessageBox.Show(msgClass.msgUncorrectData, msgClass.msgAchtung);
                    return;
                }

                region = new tb_region
                {
                    UISID = uisID,
                    Population = population,
                    Name = tbNewRegion.Text,
                    AVPQuantityBefore = avpBefore
                };
                DBClass.RESPContext.tb_regions.InsertOnSubmit(region);
                updateRec = true;
                cbRegion.Visible = true;
                this.Controls.Remove(tbNewRegion);
            }
            try
            {
                if (DBClass.RESPContext.GetChangeSet().Deletes.Count > 0 || DBClass.RESPContext.GetChangeSet().Inserts.Count > 0 || DBClass.RESPContext.GetChangeSet().Updates.Count > 0)
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                cbRegion.Items.Clear();
                cbRegion.Items.AddRange(UtilClass.FillCBRegions(uisID));
                cbRegion.Visible = true;
                tbNewRegion.Dispose();
            }
            catch
            {
                MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
            }

        }

        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            if (!updateRec)
            {
                tbNewRegion.Dispose();
                cbRegion.Visible = true;
            }
            else
            {
                this.Close();
            }

        }

    }
}
